package com.chain.voter.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chain.voter.ProgressView;
import com.chain.voter.R;
import com.chain.voter.modle.Candidate;
import com.chain.voter.modle.Vote;
import com.chain.voter.modle.VoteCard;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by baoyunfeng on 2018/6/23.
 */

public class VoteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<VoteCard> voteCardList = new ArrayList<>();

    public VoteAdapter(List<VoteCard> voteCardList) {
        this.voteCardList = voteCardList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return VoteViewHolder.createVoteViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((VoteViewHolder) holder).setData(voteCardList.get(position));
    }

    @Override
    public int getItemCount() {
        return voteCardList.size();
    }

    public static class VoteViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.rootView)
        LinearLayout rootView;

        @BindView(R.id.name)
        TextView name;

        @BindView(R.id.state)
        TextView state;

        @BindView(R.id.stime)
        TextView stime;

        @BindView(R.id.etime)
        TextView etime;

        @BindView(R.id.author)
        TextView author;

        @BindView(R.id.content)
        LinearLayout content;

        int count;

        public static VoteViewHolder createVoteViewHolder(ViewGroup parentView) {
            View view = LayoutInflater.from(parentView.getContext()).inflate(R.layout.viewholder_vote, parentView,
                    false);
            return new VoteViewHolder(view);
        }

        public VoteViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(VoteCard voteCard) {
            count = 0;
            content.removeAllViews();

            Vote vote = voteCard.getVote();
            name.setText(vote.getName());
            stime.setText("投票开始时间:" + vote.getsTime());
            etime.setText("投票结束时间:" + vote.geteTime());
            author.setText(vote.getAuthor());

            if (voteCard.getState() == 0) {
                state.setText("投票进行中");
            } else {
                state.setText("投票已结束");
            }

            for (Candidate candidate : voteCard.getCandidates()) {
                count += candidate.getValue().getNumber();
            }

            for (Candidate candidate : voteCard.getCandidates()) {
                ProgressView progressBar = new ProgressView(rootView.getContext());
                progressBar.setProcess(String.valueOf(voteCard.getKey()), candidate, count, voteCard.getState() == 0,
                        voteCard.getVote().getName());
                ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams
                        .MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                content.addView(progressBar, layoutParams);
            }
        }
    }
}
